<template>
  <div>
    <!-- 查询 -->
    <span style="color:#606266;font-size:16px;margin-right:10px">名称</span>
    <el-input class="input" v-model="input" placeholder="属性名称"></el-input>
    <span style="color:#606266;font-size:16px;margin-left:10px;margin-right:10px;">分类</span>
    <el-button type="primary" @click="search" style="margin-left:10px;">查询</el-button>
    <el-button type="primary" @click="addClick">添加</el-button>
    <!-- 表格数据 -->
    <template>
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="id" label="ID"></el-table-column>
        <el-table-column prop="name" label="品牌名称" width="180"></el-table-column>
        <el-table-column prop="floor_price" label="最低价"></el-table-column>
        <el-table-column prop="is_new" label="新品"></el-table-column>
        <el-table-column prop="is_show" label="显示"></el-table-column>
        <el-table-column fixed="right" label="操作" width="80">
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row)" type="primary" size="small">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>
    <!-- 分页 -->
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="$store.state.brand.brandList.count"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import { GetBrand } from '../../request/api'
export default {
  data() {
    return {
      input: '',
      // tableData: []
      currentPage: 1,
      pagesize: 10,
    }
  },
  computed: {
    tableData() {
      let tableData = this.$store.state.brand.brandList.data
      for (var i = 0; i < tableData.length; i++) {
        // "是"、"否"转换
        tableData[i].is_new == 0 ? tableData[i].is_new = "否" : tableData[i].is_new = "是"
        tableData[i].is_show == 0 ? tableData[i].is_show = "否" : tableData[i].is_show = "是"
      }
      // console.log("页面品牌数据", tableData)
      return tableData
    }
  },

  methods: {
    handleClick(row) {
      // console.log(row);
      this.$router.push("/editbrand/" + row.id)
    },
    // 改变每一页的条数
    handleSizeChange(currentPageSize) {
      this.getCurrentData(this.currentPage, currentPageSize, this.input)
    },
    // 改变当前页
    handleCurrentChange(currentPage) {
      this.getCurrentData(currentPage, this.pagesize, this.input)
    },

    // 点击根据关键词查询
    search() {
      // console.log("点击了", this.input)
      this.getCurrentData(this.currentPage, this.pagesize, this.input)
    },

    // 点击添加数据
    addClick() {
      // console.log("点击了添加")
      this.$router.push("/addbrand")
    },

    // 封装搜索函数
    getCurrentData(curPage, curSize, input) {
      GetBrand({
        page: curPage || 1,
        size: curSize || 10,
        name: input || ''
      }).then(res => {
        this.$store.commit('saveBrand', res.data)
      })
    }

  },

  created() {

    // 通过获取商品的api接口获取到的数据存到vuex里面
    GetBrand({
      headers: { 'X-Nideshop-Token': localStorage.getItem('token') },
      page: 1,
      size: 10
    }).then(res => {
      console.log("get/admin/brand")
      this.$store.commit('saveBrand', res.data)
    })
  }

}
</script>

<style lang="less" scoped>
.input {
  margin-bottom: 10px;
  width: 200px;
}
.el-pagination {
  text-align: right;
}
</style>